package com.lhkj.ct.meta.modules.gauge.service.impl;

import com.lhkj.ct.base.mybatis.extension.service.impl.BatchHandleServiceImpl;
import com.lhkj.ct.meta.modules.gauge.mapper.QutLogicMapper;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblLogicAsso;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblQutLogic;
import com.lhkj.ct.meta.modules.gauge.service.LogicAssoService;
import com.lhkj.ct.meta.modules.gauge.service.QutLogicService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-10-09
 */
@Service
@Deprecated
public class TblQutLogicServiceImpl extends BatchHandleServiceImpl<QutLogicMapper, TblQutLogic> implements QutLogicService {

    @Resource
    private LogicAssoService logicAssoService;

    @Override
    public boolean saveBatch(Collection<TblQutLogic> entityList) {
        boolean save = super.saveBatch(entityList, 200);
        if (!save) return false;
        List<TblLogicAsso> logicAssos = entityList.stream()
                .filter(o -> CollectionUtils.isNotEmpty(o.getLogicAssos()))
                .flatMap(o -> o.getLogicAssos().stream().peek(l -> {
                    l.setLocAssId(o.getLocId());
                }))
                .collect(Collectors.toList());
        return logicAssoService.saveBatch(logicAssos);
    }


    @Override
    public void clean(Collection<?> questionIds) {
        if (questionIds.isEmpty()) {
            return;
        }
        List<String> locIds = getBaseMapper().selectAllByLocQutId(questionIds);
        if (CollectionUtils.isNotEmpty(locIds)){
            getBaseMapper().deleteBatchIds(locIds);
            logicAssoService.removeByLocIds(locIds);
        }
    }

//    @Override
//    public boolean saveOrUpdateBatch(Collection<TblQutLogic> entityList) {
//        boolean save = super.saveOrUpdateBatch(entityList, 200);
//        if (!save) return false;
//        List<TblLogicAsso> logicAssos = entityList.stream()
//                .filter(o -> CollectionUtils.isNotEmpty(o.getLogicAssos()))
//                .flatMap(o -> o.getLogicAssos().stream().peek(l -> {
//                    l.setLocAssId(o.getLocId());
//                    l.setLocAssQutId(o.getLocQutId());
//                }))
//                .collect(Collectors.toList());
//        return logicAssoService.saveBatch(logicAssos);
//    }

    //    @Override
//    public void compareAndUpdate(TblQutLogic source, TblQutLogic target) {
//        if (!source.compare(target)) {
//            updateById(source);
//        }
//        // 收集旧的id
//        List<String> optIds = target.getLogicAssos().stream().map(TblLogicAsso::getLocAssId).collect(Collectors.toList());
//        // 按照id进行合并
//        Map<String, TblLogicAsso> optionMap = target.getLogicAssos().stream().collect(
//                Collectors.toMap
//                        (TblLogicAsso::getLocAssId, each -> each, (value1, value2) -> value1)
//        );
//        // 新增的选项
//        List<TblLogicAsso> newOptions = new ArrayList<>();
//        for (TblLogicAsso logicAsso : source.getLogicAssos()) {
//            // 新增
//            if (StringUtils.isBlank(logicAsso.getLocAssId())) {
//                newOptions.add(logicAsso);
//            }
//            optIds.remove(logicAsso.getLocAssId());
//            // 无变化
//            if (target.getLogicAssos().remove(logicAsso)) {
//                continue;
//            }
//            // 有更新
//            if (optionMap.containsKey(logicAsso.getLocAssId())){
//                logicAssoService.updateById(logicAsso);
//            }
//        }
//        logicAssoService.saveBatch(newOptions);
//        logicAssoService.removeByIds(optIds);
//    }
}
